package com.jiukuaidao.zhulin.module.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.CommContract;
import com.jiukuaidao.zhulin.module.CommPresenter;
import com.jiukuaidao.zhulin.module.CommWatcher;
import com.jiukuaidao.zhulin.module.address.bean.PreAddressBean;
import com.jiukuaidao.zhulin.module.user.contract.RegisterContract;
import com.jiukuaidao.zhulin.module.user.presenter.RegisterPresenter;
import com.jiukuaidao.zhulin.util.UserInfoUtil;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/26
 * Description : 快速注册
 */
public class RegisterActivity extends BaseActivity<RegisterContract.Presenter> implements RegisterContract.View, View.OnClickListener, CommContract.CodeView {
    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.titile_text)
    TextView mTitileText;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.bt_code_phone_register)
    TextView mBtnCode;
    @BindView(R.id.et_passwordd)
    EditText mEtPassword;
    @BindView(R.id.bt_submit)
    Button mBtnSubmit;
    @BindView(R.id.tv_register_rule)
    TextView mTvRegisterRule;
    /**
     * 当前位置
     */
    private PreAddressBean mAddressBean;

    private CommPresenter mCommPresenter;


    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_phone_register;
    }

    @Override
    protected void handleBundle(Bundle bundle) {

    }

    @Override
    protected void init() {
        mTitileText.setText(R.string.user_register_title);
        mCommPresenter = new CommPresenter(TaskProvider.provideTasksRepository(this), this);
        new RegisterPresenter(TaskProvider.provideTasksRepository(this), this);
        setOnClickListener();
        mEtPassword.addTextChangedListener(new CommWatcher(mBtnSubmit, CommWatcher.PWDLOGINBTN));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titile_left_imageview:
                finish();
                break;
            case R.id.bt_submit:
                mPresenter.start();
                break;
            case R.id.bt_code_phone_register:
                mCommPresenter.getVeriCode();
        }
    }

    public void setOnClickListener() {
        mBtnSubmit.setOnClickListener(this);
        mBtnCode.setOnClickListener(this);
        mLeftImg.setOnClickListener(this);
    }

    @Override
    public String getMobile() {
        return mEtPhone.getText().toString();
    }

    @Override
    public String getVeriCode() {
        return mEtCode.getText().toString();
    }

    @Override
    public String getPassword() {
        return mEtPassword.getText().toString();
    }

    @Override
    public String getUserIp() {
        return UserInfoUtil.getUserIp();
    }

    @Override
    public String getAppKey() {
        return UserInfoUtil.get_app_key(this);
    }

    @Override
    public void jumpActivity() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    @Override
    public String getCodeMobile() {
        return mEtPhone.getText().toString();
    }

    @Override
    public TextView codeBtn() {
        return mBtnCode;
    }

    @Override
    public void codeToast(String msg, String errorCode) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
        if (errorCode.equals("1010")) {
            GraphicCodeDialog codeDialog = new GraphicCodeDialog(RegisterActivity.this);
            codeDialog.showDialog();
        }
    }
}
